package jdbc2;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class jdbcLearn2 {
   public void isAdmin() throws Exception {
       String url = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
       String username = "root";
       String password = "123456";
       Connection connection = DriverManager.getConnection(url, username, password);
       //接受用户收到的数据
       System.out.println("------欢迎登录--------");
       System.out.println("请输入用户号");
       Scanner scanner = new Scanner(System.in);
       String name = scanner.next();
       System.out.println("请输入密码");
       String pwd = scanner.next();
       //定义sql;
       String sql = "select*from manager where admin=? and password=?";
       //获取预编译好的对象
       PreparedStatement preparedStatement = connection.prepareStatement(sql);
       //设置？的值；
       preparedStatement.setString(1, username);
       preparedStatement.setString(2, password);
       //执行sql；
       ResultSet resultSet = preparedStatement.executeQuery();
       if (resultSet.next()) {
           System.out.println("登陆成功");
       } else {
           System.out.println("非法管理员");
       }
   }




    public void insert() throws SQLException {
        String url="jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
        String username="root";
        String password="123456";
        Connection connection = DriverManager.getConnection(url, username, password);
        //接收用户的数据
       String name="abc";
       String pwd="123";

        String sql="insert into tb_user values(null,?,?)";
        PreparedStatement prestatement = connection.prepareStatement(sql);
        prestatement.setString(1,"abc");
        prestatement.setString(2,"123");

        int i = prestatement.executeUpdate();
        System.out.println("向数据库表中添加了"+i+"行数据");
    }
    public ArrayList<User> findAll() throws SQLException {
        ArrayList<User> users = new ArrayList<>();
        String url="jdbc:mysql://localhost:3308/akui88?useSSL=false&serverTimezone=UTC";
        String username="root";
        String password="12345678";
        Connection connection = DriverManager.getConnection(url, username, password);

        String sql="select * from tb_user";

        PreparedStatement prestatement = connection.prepareStatement(sql);
        ResultSet resultSet = prestatement.executeQuery();

        while(resultSet.next())
        {
            int id = resultSet.getInt("id");
            String username1 = resultSet.getString("username");
            String password1 = resultSet.getString("password");
           System.out.println(id+"-"+username1+"-"+password1);
            User user = new User();
            user.setId(id);
            user.setUsername(username1);
            user.setPassword(password1);
            users.add(user);
        }
        prestatement.close();
        connection.close();
    System.out.println("users = " + users);
        return users;


    }
}


